﻿using AutoMapper;
using Entity.BaseInfo.DTO.Customer;
using Entity.BaseInfo.Entity;
using Entity.BaseInfo.View.Customer;
using Entity.Finance.DTO.IncomeAccount;
using Entity.Finance.DTO.IncomeManage;
using Entity.Finance.Entity;
using Entity.Finance.View.IncomeManage;
using FrameWork.DataContext;
using FrameWork.DataHelper;
using FrameWork.Request;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Common.Enum.StateEnum;

namespace Service
{
    public class IncomeManageService
    {
        /**
         * 数据库
         */
        private readonly DataContext _context;

        /**
         * 自动映射
         */
        private readonly IMapper _mapper;

        /**
         * 构造注入
         */
        public IncomeManageService(DataContext context, IMapper mapper)
        {
            _context = context;
            _mapper = mapper;
        }

        /**
         * 获取列表
         */
        public async Task<RequestResponseModel<PageResult<IncomeManageView>>> GetIncomeManageList(IncomeManageSearchDTO model)
        {
            //筛选
            var query = _context.IncomeManageView
                .WhereIF(a => a.IncomeDone > 0, model.Type == 1)
                .WhereIF(a => a.CustomerCode.Contains(model.Customer) || a.CustomerName.Contains(model.Customer), !string.IsNullOrEmpty(model.Customer))
                .WhereIF(a => a.Status == model.Status, model.Status != null)
                ;

            //返回列表
            var list = await query
                .OrderByDescending(a => a.CreateTime)
                .Page(model.CurrentPage, model.PageSize)
                .ToListAsync();

            //总数据量
            var count = await query.CountAsync();

            //返回数据
            var pageResult = new PageResult<IncomeManageView>(list, count);
            return RequestResponse.CreateResponse(200, "查询成功", pageResult);
        }

        /// <summary>
        /// 收款
        /// </summary>
        public async Task<RequestResponseModel<string>> Income(IncomeManageIncomeDto model)
        {
            //校验数据
            var query = await Valid.ValidId<IncomeManageModel>(_context.IncomeManage, model.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }
            query.IncomeDone += model.Amount;
            query.File = model.File;
            query.Remark = model.Remark;
            if (query.Amount > query.IncomeDone)
            {
                query.Status = IncomeType.收款中;
            }
            else if (query.Amount <= query.IncomeDone)
            {
                query.Status = IncomeType.已收款;
            }
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "收款成功", model.Id);
        }


        /// <summary>
        /// 退款
        /// </summary>
        public async Task<RequestResponseModel<string>> Return(IncomeManageIncomeDto model)
        {
            //校验数据
            var query = await Valid.ValidId<IncomeManageModel>(_context.IncomeManage, model.Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", model.Id);
            }
            query.IncomeDone -= model.Amount;
            if (query.IncomeDone < 0)
            {
                return RequestResponse.CreateResponse(400, "退款金额超过已收款金额", model.Id);
            }
            query.ReturnFile = model.File;
            query.ReturnRemark = model.Remark;
            if (query.Amount > query.IncomeDone)
            {
                query.Status = IncomeType.收款中;
            }
            else if (query.Amount <= query.IncomeDone)
            {
                query.Status = IncomeType.已收款;
            }
            else if (query.Amount == 0)
            {
                query.Status = IncomeType.待收款;
            }
            await _context.SaveChangesAsync();
            return RequestResponse.CreateResponse(200, "退款成功", model.Id);
        }


        /**
         * 删除
         */
        public async Task<RequestResponseModel<string>> DeleteIncomeManage(string Id)
        {
            //校验数据
            var query = await Valid.ValidId<IncomeManageModel>(_context.IncomeManage, Id);
            if (query == null)
            {
                return RequestResponse.CreateResponse(400, "Id无效", Id);
            }
            if (query.IncomeDone > 0)
            {
                return RequestResponse.CreateResponse(400, "未收款的才能删除", Id);
            }
            //更新表
            _context.IncomeManage.Remove(query);
            await _context.SaveChangesAsync();

            return RequestResponse.CreateResponse(200, "删除成功", Id);
        }
    }
}
